package com.alen.sun.service.Impl;

import com.alen.sun.bean.Content;
import com.alen.sun.bean.Elderly;
import com.alen.sun.bean.Message;
import com.alen.sun.bean.PageBean;
import com.alen.sun.dao.MessageMapper;
import com.alen.sun.service.MessageService;
import com.alen.sun.util.PhoneCode;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageMapper messageMapper;

    @Override
    public void add(Elderly elderly, Content content) {
        Message message=new Message();
        message.setMessageId(PhoneCode.generateCode());
        message.setContent(content.getDetails());
        message.setStatus(0);
        message.setElderlyId(elderly.getElderlyId());
        messageMapper.add(message);
    }

    @Override
    public PageBean list(Integer pageNum, Integer pageSize) {
        //1、设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        //2、执行查询
        List<Message> messages=messageMapper.list();
        Page<Message> p=(Page<Message>)messages;
        //3、封装PageBean对象
        PageBean pageBean=new PageBean(p.getTotal(),p.getResult());
        log.info("{}",pageBean);
        return pageBean;
//        return messageMapper.list();
    }

    @Override
    public Boolean updateStatus(String id) {
        Boolean flag=messageMapper.updateStatus(id);
        return flag;
    }

    @Override
    public List<Message> getMessagesForCaregiver(String employeeId) {
        return messageMapper.selectMessagesByEmployeeId(employeeId);
    }

    @Override
    public PageBean page(Integer page, Integer pageSize, String word) {
        //1. 设置分页参数
        PageHelper.startPage(page,pageSize);
        List<Message> messages;
        //2. 执行查询
        if (word==null||word.equals("")){
            messages=messageMapper.list();
        }else {
            word = "%" + word + "%";
            messages = messageMapper.selectByWord(word);
        }
        Page<Message> p = (Page<Message>) messages;

        //3. 封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public Boolean delete(String id) {
        Boolean flag=messageMapper.delete(id);
        return flag;
    }

    @Override
    public Boolean deleteByIds(List<String> ids) {
        Boolean flag = messageMapper.deleteByIds(ids);
        return flag;
    }
}
